package com.lelefly.base.serviceImp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.engine.spi.RowSelection;
import org.springframework.stereotype.Service;

import com.lelefly.base.daoimp.BaseDao;
import com.lelefly.base.daoimp.InsuranceDao;
import com.lelefly.base.model.Result;
import com.lelefly.base.model.RowResult;
import com.lelefly.base.model.SearchInsuranceBean;
import com.lelefly.base.po.LeleflyCompanyInsurance;
import com.lelefly.base.po.LeleflyPersonalInsurance;
import com.lelefly.constant.Constant;

@Service
public class InsuranceService extends BaseService {
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	@Resource
	private BaseDao baseDao;

	@Resource
	private InsuranceDao insuranceDao;

	/**
	 * 增加个人保单
	 * 
	 * @param insurance
	 */
	public Result addPersonalInsurance(LeleflyPersonalInsurance insurance) {
		Result result = new Result<>();
		insurance.setAddDate(sdf.format(new Date()));
		insurance.setInsuranceState(Constant.INSURANCE_UNHANDLED);
		insurance.setInsuranceNumber(insurance.getPhonenumber()
				+ System.currentTimeMillis() + "");
		try {
			baseDao.saveObject(insurance);
			result.setMes("新增成功");
			result.setState(Constant.HTTP_STATUS_OK);
		} catch (Exception e) {
			result.setMes("数据库保存失败");
			result.setState(Constant.HTTP_STATUS_FAIL);

			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 增加公司保单
	 * 
	 * @param insurance
	 * @return
	 */
	public Result addCompanyInsurance(LeleflyCompanyInsurance insurance) {
		Result result = new Result<>();
		insurance.setAddDate(sdf.format(new Date()));
		insurance.setInsuranceState(Constant.INSURANCE_UNHANDLED);
		insurance.setInsuranceNumber(insurance.getPhonenumber()
				+ System.currentTimeMillis() + "");
		try {
			baseDao.saveObject(insurance);
			result.setMes("新增企业保单成功");
			result.setState(Constant.HTTP_STATUS_OK);
		} catch (Exception e) {
			result.setMes("数据库保存失败");
			result.setState(Constant.HTTP_STATUS_FAIL);

			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 根据手机号查询个人保单信息
	 * 
	 * @param phoneNo
	 * @return
	 */
	public Result<SearchInsuranceBean> getInsuranceInfo(String phoneNo) {
		Result<SearchInsuranceBean> result = new Result<>();
		String sql = "select insurance_number AS insuranceNo , "
				+ "begin_time AS beginDate,end_time AS endDate,"
				+ "insurance_state AS status,1 as type "
				+ "from lelefly_personal_insurance where phonenumber='"
				+ phoneNo
				+ "'"
				+ "union "
				+ "SELECT insurance_number AS insuranceNo , "
				+ "begin_time AS beginDate,end_time AS endDate,"
				+ "insurance_state AS status,2 as type FROM lelefly_company_insurance WHERE phonenumber='"
				+ phoneNo + "'";
		List resultList = insuranceDao.getInsuranceInfo(sql);
		result.setResultList(resultList);
		result.setState(Constant.HTTP_STATUS_OK);
		result.setMes("查询成功");
		return result;
	}

	public Result<SearchInsuranceBean> queryInsuranceInfo(String planeId) {
		Result<SearchInsuranceBean> result = new Result<>();
		String sql = "select insurance_number AS insuranceNo , "
				+ "begin_time AS beginDate,end_time AS endDate,"
				+ "insurance_state AS status,1 as type "
				+ "from lelefly_personal_insurance where plane_id='"
				+ planeId
				+ "'"
				+ "union "
				+ "SELECT insurance_number AS insuranceNo , "
				+ "begin_time AS beginDate,end_time AS endDate,"
				+ "insurance_state AS status,2 as type FROM lelefly_company_insurance WHERE plane_id='"
				+ planeId + "'";
		List resultList = insuranceDao.getInsuranceInfo(sql);
		result.setResultList(resultList);
		result.setState(Constant.HTTP_STATUS_OK);
		result.setMes("查询成功");
		return result;
	}

	public RowResult<LeleflyCompanyInsurance> queryAllInsurance(String limit,
			String start) {
		int page = Integer.valueOf(start);
		int count = Integer.valueOf(limit);
		RowResult<LeleflyCompanyInsurance> result = new RowResult<>();
		String hql = "select id,insurance_number,companyname,social_credit_code,username,"
				+ "phonenumber,idno,email,begin_time,end_time,flyer_name,flyer_id_no,add_date,insurance_state,plane_id,plane_name from lelefly_company_insurance union all "
				+ " select id,insurance_number,'个人' AS companyname,' ' AS social_credit_code"
				+ ",username,phonenumber,idno,email,begin_time,end_time,flyer_name,flyer_id_no,add_date,insurance_state,plane_id,plane_name from lelefly_personal_insurance ";
		List<LeleflyCompanyInsurance> list = insuranceDao.queryAllInsurance(
				hql, page, count);
		result.setRows(list);
		result.setTotal(baseDao.queryObjetReListBySql(hql, LeleflyCompanyInsurance.class).size());
		return result;
	}
}
